class Solution {
public:
    string minWindow(string s, string t) {
        int slen = s.size(), tlen = t.size(), left = 0, right = 0, count = 0;
        int kinds = 0; // 统计有效字符的种类
        int hash1[128] = {0};
        int hash2[128] = {0};
        for (auto ch : t)
        {
            if (hash1[ch]++ == 0)
                kinds++;
        }
            
        int minlen = INT_MAX, begin = -1;
        while (right < s.size()) {
            // 进窗口
            char in = s[right];
            if (++hash2[in] == hash1[in])
                count++;
            while(count == kinds) {
                if (right - left + 1 < minlen) {
                    minlen = right - left + 1;
                    begin = left;
                }
                char out = s[left++];
                if (hash2[out]-- == hash1[out])
                    count--;
            }
            
            right++;
        }
        if(begin==-1) return "";
        else return s.substr(begin,minlen);
       
    }
};
